<template>
  <j-modal
    :title="title"
    :width="600"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="群组名称">
          <a-input v-model="form.labelName" placeholder="请输入" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" @click="handleOk">确定</a-button>
    </template>
  </j-modal>
</template>

<script>
import * as peopleApi from '@/api/wxSmuggle/people.js'
export default {
  name: 'groupsHandkeModal',
  props: {
    title: {
      type: String,
      default: '新建群组'
    },
  },
  data() {
    return {
      visible: false,
      disableSubmit: false,
      form: {},
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      confirmLoading: false
    }
  },
  methods: {
    // 新建群组、新建下级群组
    add(record) {
      this.visible = true
      if(record && Object.keys(record).length>0) {
        this.$set(this.form, 'parentId', record.value)
      } else {
        this.$set(this.form, 'parentId', '0')
      }
      this.$nextTick(() => {})
    },
    // 修改分组
    edit(record) {
      this.visible = true
      this.$set(this.form, 'id', record.value)
      this.$set(this.form, 'labelName', record.title)
      this.$nextTick(() => {})
    },
    close() {
      this.$emit('close')
      this.form = {}
      this.visible = false
    },
    handleOk() {
      let obj = { labelType: 2 }
      let item = Object.assign({}, obj, this.form)
      let api
      // 新建群组、新建下级群组
      if(item && Object.keys(item).length>0 && item.parentId) {
        api = peopleApi.labelTreeAdd(item)
      // 修改分组
      } else if(item && Object.keys(item).length>0 && item.id) {
        api = peopleApi.labelTreeEdit(item)
      }
      api.then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.handleCancel()
          this.$emit('ok');
        } else {
          this.$message.warn(res.message)
        }
      }).catch(e => {
        this.$message.warn('操作失败！')
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style lang="less" scoped></style>